package cn.edu.hqu.cst.android.exe4;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public static int MODE = MODE_PRIVATE;
    public static final String PREFERENCE_NAME = "Savesetting";
    public static final String KEY_ACCOUNT = "account";
    public static final String KEY_PASSWORD = "password";
    Button btnLogin;
    EditText etAccount, etPassword;
    SharedPreferences sharedPreferences;
    boolean isLogin = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnLogin = findViewById(R.id.btn_login);
        etAccount = findViewById(R.id.account_input);
        etPassword = findViewById(R.id.password_input);
        btnLogin.setOnClickListener(this);
        // 初始化SharedPreferences
        sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE);
        String account = sharedPreferences.getString("account", "");
        String password = sharedPreferences.getString("password", "");
        etAccount.setText(account);
        etPassword.setText(password);
        // 为按钮添加点击监听器
        btnLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_login) {
            String account = etAccount.getText().toString();
            String password = etPassword.getText().toString();
            // 如果输入的账户和密码不为空，则保存到SharedPreferences
            if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(password)) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("isLogin", true);
                editor.putString("account", account);
                editor.putString("password", password);
                editor.apply();
                isLogin = true;
                // 进入主界面
                showHomeScreen();
            } else {
                // 提示用户输入不可为空
                Toast.makeText(this, "帐号和密码不能为空", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void showHomeScreen() {
        // 设置主界面的布局
        setContentView(R.layout.activity_login);
        // 获取返回按钮并设置点击监听器
        Button btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(v -> {
            // 返回MainActivity
            showMainScreen();
        });
    }

    private void showMainScreen() {
        // 设置主界面的布局
        setContentView(R.layout.activity_main);
        // 重新获取布局中的按钮和输入框
        btnLogin = findViewById(R.id.btn_login);
        etAccount = findViewById(R.id.account_input);
        etPassword = findViewById(R.id.password_input);
        // 读取SharedPreferences中保存的账户和密码
        String account = sharedPreferences.getString("account", "");
        String password = sharedPreferences.getString("password", "");
        etAccount.setText(account);
        etPassword.setText(password);
        // 为按钮添加点击监听器
        btnLogin.setOnClickListener(this);
    }
}